package com.azi.baseconhecimento.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.azi.baseconhecimento.entity.Dica;

@Transactional
@Repository
@SuppressWarnings("unchecked")
public class DicaDao {

	EntityManager em;

	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.em = entityManager;
	}

	public Dica salvar(Dica dica) {
		return forcarLazy(em.merge(dica));
	}

	public List<Dica> buscarDicas(int maxResults) {
		Query query = em.createQuery("SELECT d FROM Dica d ORDER BY d.dataHora DESC");
		query.setMaxResults(maxResults);

		try {
			return forcarLazy(query.getResultList());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public List<Dica> buscarDicasPorTitulo(String titulo) {
		Query query = em.createQuery("SELECT d FROM Dica d WHERE d.titulo LIKE :titulo ORDER BY d.titulo");
		query.setParameter("titulo", "%" + titulo + "%");

		try {
			return forcarLazy(query.getResultList());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public List<Dica> buscarDicasPorTituloDescricao(String tituloDescricao) {
		Query query = em.createQuery("SELECT d FROM Dica d WHERE (d.titulo LIKE :tituloDescricao OR d.descricao LIKE :tituloDescricao) ORDER BY d.titulo");
		query.setParameter("tituloDescricao", "%" + tituloDescricao + "%");

		try {
			return forcarLazy(query.getResultList());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public List<Dica> buscarDicasPorColaborador(String colaborador) {
		Query query = em.createQuery("SELECT d FROM Dica d WHERE d.colaborador LIKE :colaborador ORDER BY d.titulo");
		query.setParameter("colaborador", "%" + colaborador + "%");

		try {
			return forcarLazy(query.getResultList());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public Dica buscarDica(Integer id) {
		Query query = em.createQuery("SELECT d FROM Dica d WHERE d.id = :id");
		query.setParameter("id", id);

		Dica dica = null;

		try {
			dica = (Dica) query.getSingleResult();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return forcarLazy(dica);
	}

	public List<Dica> buscarDicasPorCategoria(String categoria) {
		Query query = em.createQuery("SELECT d FROM Dica d JOIN d.categorias c WHERE c.nome = :categoria ORDER BY d.titulo");
		query.setParameter("categoria", categoria);

		try {
			return forcarLazy(query.getResultList());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public List<Dica> listarDicas() {
		Query query = em.createQuery("select d from Dica d ORDER BY d.dataHora DESC");

		try {
			return forcarLazy(query.getResultList());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private List<Dica> forcarLazy(List<Dica> dicas) {
		if (dicas != null) {
			for (Dica dica : dicas) {
				if (dica != null) {
					if (dica.getComentarios() != null) {
						dica.getComentarios().size();
					}
				}

				if (dica != null) {
					if (dica.getCategorias() != null) {
						dica.getCategorias().size();
					}
				}
			}
		}

		return dicas;
	}

	private Dica forcarLazy(Dica dica) {
		if (dica != null) {
			if (dica.getComentarios() != null) {
				dica.getComentarios().size();
			}
		}

		if (dica != null) {
			if (dica.getCategorias() != null) {
				dica.getCategorias().size();
			}
		}

		return dica;
	}
}
